#include "newsimdevicedialog.h"
#include "ui_newsimdevicedialog.h"
#include "devicelistdialog.h"
#include "selectprotocoldialog.h"
#include "IDevicePlugin.h"
#include "confighelper.h"
#include "qsoftcore.h"
#include "qprojectcore.h"
#include "qpropertyfactory.h"
#include "property/qabstractproperty.h"
#include "qpropertylist/qpropertylistview.h"
#include <QDir>
#include <QDialog>
#include <QFileDialog>
#include <QMessageBox>
#include <QApplication>
#include <QSettings>
#include <QFile>
#include <QPluginLoader>
#include "devicepluginloader.h"
#include <QDebug>


NewSimDeviceDialog::NewSimDeviceDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::NewSimDeviceDialog)
{
    ui->setupUi(this);
}

NewSimDeviceDialog::~NewSimDeviceDialog()
{
    delete ui;
}

void NewSimDeviceDialog::on_btnHelp_clicked()
{

}



void NewSimDeviceDialog::on_btnCheck_clicked()
{
    if(check_data()) {
        QMessageBox::information(this, tr("系统提示"), tr("设置正确！"));
    }
}

void NewSimDeviceDialog::on_btnOk_clicked()
{
    if(check_data()) {
        QDialog::accept();
    }
}

void NewSimDeviceDialog::on_btnExit_clicked()
{
    QDialog::reject();
}



bool NewSimDeviceDialog::check_data()
{
    bool ret = true;
#if 0
    if(ui->editProjectName->text().isEmpty()) {
        QMessageBox::information(this, tr("系统提示"), tr(""));
        ret = false;
    }
#endif
    return ret;
}




void NewSimDeviceDialog::load(const QString &szName){
    if(szName == "") {
        return;
    }
    DeviceInfo &deviceInfo = QSoftCore::getCore()->getProjectCore()->m_deviceInfo;
    DeviceInfoObject *pObj = deviceInfo.getObjectByName(szName);
    if(pObj == NULL) {
        return;
    }
    ui->deviceName->setText(pObj->m_name);
    m_szPluginName = pObj->m_deviceName;
    if(pObj->m_frameTimePeriod>0) ui->timeRate->setDisplayIntegerBase(pObj->m_frameTimePeriod);
    if(pObj->m_frameLen>0) ui->timeContinued->setDisplayIntegerBase(pObj->m_frameLen);
    if(!pObj->m_protocol.isEmpty()) ui->dataGenRule->setText(pObj->m_protocol);

    SimDevice simDev;
    simDev.fromString(pObj->m_portParameters);
    ui->deviceName->setText(simDev.m_deviceName);
    m_szPluginName = simDev.m_deviceName;
    if(simDev.m_frameTimePeriod>0) ui->timeRate->setDisplayIntegerBase(simDev.m_frameTimePeriod);
    if(simDev.m_frameLen>0) ui->timeContinued->setDisplayIntegerBase(simDev.m_frameLen);
    if(!simDev.m_protocol.isEmpty()) ui->dataGenRule->setText(simDev.m_protocol);

    QString pluginName = pObj->m_protocol;
    IDevicePlugin *pDevPluginObj = DevicePluginLoader::getInstance()->getPluginObject(pluginName);
    if (pDevPluginObj) {
        if(pObj->m_properties == "") {
            pDevPluginObj->getDefaultDeviceProperty(m_properties);
        } else {
            pDevPluginObj->readProperties(pObj->m_properties, m_properties);
        }
        pDevPluginObj->getDefaultDevicePropertyDataType(m_propType);
    }
    this->updatePropertyEditor();
}

void NewSimDeviceDialog::save(const QString &szName){
    qDebug() << "NewSimDeviceDialog::save:" << szName;
    DeviceInfo &deviceInfo = QSoftCore::getCore()->getProjectCore()->m_deviceInfo;
    DeviceInfoObject *pObj = deviceInfo.getObjectByName(szName);

    if(pObj == NULL) {
        pObj = deviceInfo.newObject();
        if(pObj == NULL) {
            return;
        }
    }
    qDebug() << "NewSimDeviceDialog::save2:" << szName;

    pObj->m_deviceType = "SIM";
    pObj->m_name = ui->deviceName->text();
    pObj->m_deviceName = m_szPluginName;
    pObj->m_frameLen = ui->timeContinued->value();
    pObj->m_frameTimePeriod = ui->timeRate->value();
    pObj->m_protocol = ui->dataGenRule->toPlainText();
    qDebug() << "NewSimDeviceDialog::save3:" << szName;

    SimDevice simDev;
    simDev.m_timeRate = ui->timeRate->value();
    simDev.m_timeContinued = ui->timeContinued->value();
    simDev.m_frameTimePeriod = ui->timeRate->value();
    simDev.m_frameLen = ui->timeContinued->value();
    simDev.m_alogScript = ui->dataGenRule->toPlainText();
    simDev.m_protocol = ui->dataGenRule->toPlainText();
    simDev.m_deviceType = "SIM";
    qDebug() << "NewSimDeviceDialog::save4:" << szName;

    pObj->m_portParameters = simDev.toString();

    QString pluginName = pObj->m_deviceName;
    IDevicePlugin *pDevPluginObj = DevicePluginLoader::getInstance()->getPluginObject(pluginName);
    if (pDevPluginObj) {
        pDevPluginObj->writeProperties(pObj->m_properties, m_properties);
    }
    qDebug() << "NewSimDeviceDialog::save5:" << szName;

}

QString NewSimDeviceDialog::GetDeviceName() const
{

}
QString NewSimDeviceDialog::pluginName() const
{

}
// 更新PropertyEditor数据
void NewSimDeviceDialog::updatePropertyEditor(){
    if(m_properties.count() != m_propType.count()) {
        return;
    }

    QList<QAbstractProperty *> listProperties;
    QAbstractProperty* pProObj = NULL;

    for (int i = 0; i < m_properties.size(); ++i) {
        QString szKey = m_properties[i].first;
        QString szValue = m_properties[i].second;
        QString szType = m_propType[i].second;

        if(szType == QString("int")) {
            pProObj = QPropertyFactory::create_property("Number");
            if(pProObj != NULL) {
                pProObj->setObjectProperty("name", szKey);
                pProObj->setAttribute("show_name", szKey);
                pProObj->setAttribute("group", "Attributes");
                pProObj->setAttribute(ATTR_CAN_SAME, true);
                QVariant val;
                val.setValue(szValue);
                pProObj->setValue(val);
                listProperties.append(pProObj);
            }
        } else if(szType == QString("bool")) {
            pProObj = QPropertyFactory::create_property("Bool");
            if(pProObj != NULL) {
                pProObj->setObjectProperty("name", szKey);
                pProObj->setAttribute("show_name", szKey);
                pProObj->setAttribute("group", "Attributes");
                pProObj->setAttribute(ATTR_CAN_SAME, true);
                QVariant val;
                val.setValue(szValue);
                pProObj->setValue(val);
                listProperties.append(pProObj);
            }
        }
    }

    m_propertyView->setPropertys(listProperties);
}
void NewSimDeviceDialog::onPropertyEdit(QAbstractProperty *pro, const QVariant &value)
{
    if(m_properties.count() != m_propType.count()) {
        return;
    }

    QString id = pro->getObjectProperty("name").toString();
    QString szType = getValue2ByValue1(id, m_propType);

    if(szType == QString("int")) {
        setValue2ByValue1(id, value.toString(), m_properties);
    } else if(szType == QString("bool")) {
        bool val = value.toBool();
        QString szVal = val ? "true" : "false";
        setValue2ByValue1(id, szVal, m_properties);
    }

    pro->setValue(value);
}
QString NewSimDeviceDialog::getValue2ByValue1(const QString &szVal1,
                                              QVector<QPair<QString, QString>>& properties)
{
    for (int i = 0; i < properties.size(); ++i) {
        if (properties[i].first == szVal1) {
            return properties[i].second;
        }
    }
    return "";
}


void NewSimDeviceDialog::setValue2ByValue1(const QString &szVal1,
                                           const QString &szVal2,
                                           QVector<QPair<QString, QString>>& properties)
{
    for (int i = 0; i < properties.size(); ++i) {
        if (properties[i].first == szVal1) {
            properties[i].second = szVal2;
        }
    }
}



